/**
 *
 * 请求学习词汇列表
 *
 */
package com.ilovetolearn.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.serialization.json.JSON;
	import com.ilovetolearn.business.InitializeSentenceDelegate;
	import com.ilovetolearn.model.ModelLocator;
	import com.ilovetolearn.vo.SentenceVo;

	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class InitializeSentenceCommand implements ICommand, IResponder
	{
		private var _model:ModelLocator=ModelLocator.getInstance();
		private var delegate:InitializeSentenceDelegate;

		public function InitializeSentenceCommand()
		{
			delegate=new InitializeSentenceDelegate(this);
		}

		public function execute(event:CairngormEvent):void
		{
			delegate.initializeSentence(event);
		}

		public function result(data:Object):void
		{
			var sentenceData:String=String(ResultEvent(data).result);
			var json:Object=new Object();
			json=JSON.decode(sentenceData);
			trace(sentenceData);
			var resultcode:String=json.resultcode;
			trace("resultcode=" + resultcode);
			var errormsg:String=json.errormsg;
			trace("errormsg=" + errormsg);
			if (resultcode == "0")
			{
				if (_model.sentenceAc == null)
				{
					_model.sentenceAc=new ArrayCollection();
				}
				_model.sentenceAc.removeAll();
				var sentences:Object=json.sentences;
				var sentencesArr:Array=sentences as Array;
				for (var i:Number=0; i < sentencesArr.length; i++)
				{
					var sentence:Object=sentencesArr[i];
					var sentenceid:String=sentence.sentenceid;
					var cursentence:String=sentence.cursentence;
					var pinyin:String=sentence.pinyin;
					var ressentence:String=sentence.ressentence;
					var pictureurl:String=sentence.pictureurl;
					var soundurl:String=sentence.soundurl;
					var level:String=sentence.level;
					var sentenprogress:String=sentence.sentenprogress;
					var mistakes:String=sentence.mistakes;
					var blankword:Array=sentence.blankword as Array;
					var blankwordnumber:Array=sentence.blankwordnumber as Array;
					var sentenceVo:SentenceVo=new SentenceVo();
					sentenceVo.sentenceid=sentenceid;
					sentenceVo.cursentence=cursentence;
					sentenceVo.ressentence=ressentence;
					sentenceVo.pinyin=pinyin;
					sentenceVo.sentencepic=_model.site + pictureurl;
					sentenceVo.sentencesound=soundurl;
					sentenceVo.progress=sentenprogress;
					sentenceVo.mistakes=mistakes;
					sentenceVo.level=level;
					sentenceVo.blankword=new ArrayCollection(blankword);
					sentenceVo.blankwordnumber=new ArrayCollection(blankwordnumber);
					_model.sentenceAc.addItem(sentenceVo);
				}
				_model.curState="DictationStudyState";
			}
			else
			{
				Alert.show(errormsg);
			}
		}

		public function fault(info:Object):void
		{
			Alert.show(FaultEvent(info).toString());
		}
	}
}